package com.ruoyi.buy.service.impl;

import com.ruoyi.buy.domain.entity.PO.BuyPurchaseBill;
import com.ruoyi.buy.domain.entity.PO.BuyPurchaseBillItem;
import com.ruoyi.buy.domain.entity.PO.BuyRequestbuyBillItem;
import com.ruoyi.buy.domain.entity.buy0003.Buy0003GetInfoByIdVO;
import com.ruoyi.buy.domain.entity.buy0010.Buy0010TableData;
import com.ruoyi.buy.domain.entity.buy0011.Buy0011TableData;
import com.ruoyi.buy.mapper.PO.BuyPurchaseBillItemMapper;
import com.ruoyi.buy.mapper.PO.BuyPurchaseBillMapper;
import com.ruoyi.buy.mapper.PO.BuyRequestbuyBillItemMapper;
import com.ruoyi.buy.mapper.PO.buy0009.Buy0009Mapper;
import com.ruoyi.buy.mapper.PO.buy0010.Buy0010Mapper;
import com.ruoyi.buy.mapper.PO.buy0011.Buy0011Mapper;
import com.ruoyi.buy.service.IBuy0009Service;
import com.ruoyi.buy.service.IBuy0011Service;
import com.ruoyi.buy.service.ICommonService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 参数配置 服务层实现
 * 
 * @author ruoyi
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class Buy0009ServiceImpl implements IBuy0009Service
{

    @Resource
    private Buy0009Mapper buy0009Mapper;

    @Autowired
    ICommonService commonService;

    @Resource
    private BuyRequestbuyBillItemMapper buyRequestbuyBillItemMapper;

    @Resource
    private BuyPurchaseBillMapper buyPurchaseBillMapper;

    @Resource
    private BuyPurchaseBillItemMapper buyPurchaseBillItemMapper;

    @Override
    public List<Buy0011TableData> getList(Buy0011TableData param) {

        return this.buy0009Mapper.getList(param,commonService.getUsername(),commonService.getLoginUser().getUserId());
    }

    @Override
    public void handleToInv(List<Buy0011TableData> list) throws Exception {
        for (Buy0011TableData item:list
             ) {
         BuyRequestbuyBillItem buyRequestbuyBillItem = buyRequestbuyBillItemMapper.selectBuyRequestbuyBillItemById(item.getId());
         if (!buyRequestbuyBillItem.getStatus().equals("0")){
             throw new Exception(buyRequestbuyBillItem.getvSpaName() + "不是初始状态，请确认");
         }else{
             buyRequestbuyBillItem.setStatus("1");
             buyRequestbuyBillItem.setUpdateBy(commonService.getUsername());
             buyRequestbuyBillItem.setUpdateTime(DateUtils.getNowDate());
             this.buyRequestbuyBillItemMapper.updateBuyRequestbuyBillItem(buyRequestbuyBillItem);
         }
        }
    }


    /**
     * 新增【请填写功能名称】
     *
     * @param para 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertBuyPurchaseBill(Buy0003GetInfoByIdVO para) throws Exception {
        BuyPurchaseBill buyPurchaseBill = para.getBillPO();
        buyPurchaseBill.setvType("02");
        buyPurchaseBill.setCreateTime(DateUtils.getNowDate());
        buyPurchaseBill.setCreateBy(para.getCreateBy());
        buyPurchaseBillMapper.insertBuyPurchaseBill(buyPurchaseBill);

        //反查主表Id
        buyPurchaseBill = new BuyPurchaseBill();
        buyPurchaseBill.setvBillNo( para.getBillPO().getvBillNo());
        List<BuyPurchaseBill>bills = buyPurchaseBillMapper.selectBuyPurchaseBillList(buyPurchaseBill);
        if (!bills.isEmpty() && bills.get(0).getId() != null){
            Long nMainId = bills.get(0).getId();
            List<BuyPurchaseBillItem> itemList = para.getItemPOList();
            for (BuyPurchaseBillItem item:itemList
            ) {
                //先处理合同明细
                BuyPurchaseBillItem newItem = new BuyPurchaseBillItem();
                BeanUtils.copyBeanProp(newItem,item);
                newItem.setnMainId(nMainId);
                newItem.setCreateTime(DateUtils.getNowDate());
                newItem.setCreateBy(para.getCreateBy());
                newItem.setId(null);
                newItem.setUpdateTime(null);
                newItem.setUpdateBy(null);
                newItem.setCreateBy(commonService.getUsername());
                newItem.setUpdateTime(DateUtils.getNowDate());
                newItem.setnInvQty(BigDecimal.ZERO);
                newItem.setnPostQty(BigDecimal.ZERO);
                newItem.setnArrivedQty(BigDecimal.ZERO);
                newItem.setnINWHQty(BigDecimal.ZERO);
                buyPurchaseBillItemMapper.insertBuyPurchaseBillItem(newItem);
                if (newItem.getnPrice() != null && newItem.getnPrice().compareTo(BigDecimal.ZERO) > 0){
                    this.commonService.changeSpaPrice(buyPurchaseBill,newItem,para.getCreateBy(),buyPurchaseBill.getvBillNo(),"BUY0003",buyPurchaseBill.getvType());
                }

                //合同明细成功之后，标记请购单明细完成
                BuyRequestbuyBillItem buyRequestbuyBillItem = this.buyRequestbuyBillItemMapper.selectBuyRequestbuyBillItemById(item.getId());
                buyRequestbuyBillItem.setStatus("4");
                this.buyRequestbuyBillItemMapper.updateBuyRequestbuyBillItem(buyRequestbuyBillItem);
            }
        }
        return 1;
    }

    @Override
    public int updateBuyPurchaseBill(Buy0003GetInfoByIdVO para) throws Exception {
        BuyPurchaseBill buyPurchaseBill = para.getBillPO();
        buyPurchaseBill.setUpdateTime(DateUtils.getNowDate());
        buyPurchaseBill.setUpdateBy(commonService.getUsername());
        buyPurchaseBillMapper.updateBuyPurchaseBill(buyPurchaseBill);

        //反查主表Id
        buyPurchaseBill = new BuyPurchaseBill();
        buyPurchaseBill.setvBillNo( para.getBillPO().getvBillNo());
        List<BuyPurchaseBill>bills = buyPurchaseBillMapper.selectBuyPurchaseBillList(buyPurchaseBill);
        if (!bills.isEmpty() && bills.get(0).getId() != null){
            Long nMainId = bills.get(0).getId();
            List<BuyPurchaseBillItem> itemList = para.getItemPOList();
            for (BuyPurchaseBillItem item:itemList
            ) {
                //这部分是新增的
                if(item.getId() == null || item.getId() == 0){
                    //先处理合同明细
                    BuyPurchaseBillItem newItem = new BuyPurchaseBillItem();
                    BeanUtils.copyBeanProp(newItem,item);
                    newItem.setnMainId(nMainId);
                    newItem.setCreateTime(DateUtils.getNowDate());
                    newItem.setCreateBy(para.getCreateBy());
                    newItem.setId(null);
                    newItem.setUpdateTime(null);
                    newItem.setUpdateBy(null);
                    newItem.setnInvQty(BigDecimal.ZERO);
                    newItem.setnPostQty(BigDecimal.ZERO);
                    newItem.setnArrivedQty(BigDecimal.ZERO);
                    newItem.setnINWHQty(BigDecimal.ZERO);
                    buyPurchaseBillItemMapper.insertBuyPurchaseBillItem(newItem);
                    if (newItem.getnPrice() != null && newItem.getnPrice().compareTo(BigDecimal.ZERO) > 0){
                        this.commonService.changeSpaPrice(buyPurchaseBill,newItem,para.getCreateBy(),buyPurchaseBill.getvBillNo(),"BUY0003",buyPurchaseBill.getvType());
                    }

                    //合同明细成功之后，标记请购单明细完成
                    BuyRequestbuyBillItem buyRequestbuyBillItem =
                            this.buyRequestbuyBillItemMapper.selectBuyRequestbuyBillItemById(item.getRequestItemId());
                    buyRequestbuyBillItem.setStatus("4");
                    this.buyRequestbuyBillItemMapper.updateBuyRequestbuyBillItem(buyRequestbuyBillItem);
                }else{
                    item.setUpdateTime(DateUtils.getNowDate());
                    item.setUpdateBy(commonService.getUsername());
                    buyPurchaseBillItemMapper.updateBuyPurchaseBillItem(item);
                    if (item.getnPrice() != null && item.getnPrice().compareTo(BigDecimal.ZERO) > 0){
                        this.commonService.changeSpaPrice(buyPurchaseBill,item,para.getCreateBy(),buyPurchaseBill.getvBillNo(),"BUY0003",buyPurchaseBill.getvType());
                    }
                }
            }
        }
        return 1;
    }

    @Override
    public List<Map<String, String>> getContracts() {
        BuyPurchaseBill buyPurchaseBill = new BuyPurchaseBill();
        buyPurchaseBill.setStatus("0");
        List<BuyPurchaseBill> buyPurchaseBills = this.buyPurchaseBillMapper.selectBuyPurchaseBillList(buyPurchaseBill);
        List<Map<String, String>> list= new ArrayList<>();
        for (BuyPurchaseBill item:buyPurchaseBills
             ) {
            Map<String, String> map = new HashMap<>();
            map.put("value",item.getvBillNo());
            map.put("label",item.getvBillNo());
            list.add(map);
        }
        return list;
    }

    @Override
    public Buy0003GetInfoByIdVO getPurchaseBillInfo(String vBillNo) throws Exception {
        Buy0003GetInfoByIdVO result = new Buy0003GetInfoByIdVO();
        BuyPurchaseBill buyPurchaseBill = new BuyPurchaseBill();
        buyPurchaseBill.setvBillNo(vBillNo);
        List<BuyPurchaseBill> buyPurchaseBills = this.buyPurchaseBillMapper.selectBuyPurchaseBillList(buyPurchaseBill);

        if (!("0").equals(buyPurchaseBills.get(0).getStatus())){
            throw new Exception("合同状态已经改变，请重新确认");
        }
        result.setBillPO(buyPurchaseBills.get(0));

        BuyPurchaseBillItem buyPurchaseBillItem = new BuyPurchaseBillItem();
        buyPurchaseBillItem.setnMainId(buyPurchaseBills.get(0).getId());
        List<BuyPurchaseBillItem> itemList = this.buyPurchaseBillItemMapper.selectBuyPurchaseBillItemList(buyPurchaseBillItem);
        result.setItemPOList(itemList);


        return result;
    }
}
